<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html class="win-frame">
<head>
<title>${name_china}管理-新建${name_china}</title>
 <%@include file="/common/head.jsp" %>

</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
            <span class="split"></span>
            <span>新建${name_china}</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="addForm" method="post">
  <div id="left" style="width:80%;float:left">
	 <div class="form-wrapper">
		<div class="h4 distance-double">
	        <span class="h4-title"><i class="icon icon-setting"></i>基本信息</span>
	        <span class="h4-border"></span>
	    </div>
	     <div id="addNew">
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>红包名</p>
	        <input type="text" id="name0" name="name0" class="summer-input distance-right"  /> 
	        <input type="hidden" id="coupons" name="coupons" class="summer-input summer-validatebox"  data-validatebox="validType:['required','maxLength[50]']" /> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>领取次数</p>
	        <input type="text" id="time0" name="time0" class="summer-input distance-right" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>金额</p>
	        <input type="text" id="amount0" name="amount0" class="summer-input" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>使用条件</p>
	        <input type="text" id="useCond0" name="useCond0" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>起始时间</p>
	        <input type="text" id="startTime0" name="startTime0" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>结束时间</p>
	        <input type="text" id="endTime0" name="endTime0" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>图片路径</p>
	        <input type="hidden" id="imagePath0" name="imagePath0" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>共有标识</p>
	        <input type="text" id="publicSign0" name="publicSign0" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>数量</p>
	        <input type="text" id="count0" name="count0" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>类型</p>
	        <input type="text" id="type0" name="type0" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	      
	    </div>
	 <div class="form-btn-wrapper" id="funcbar">
	 	<a class="btn btn-normal" id="addBtn">添加</a>
        <a class="btn btn-normal" id="backBtn">关闭</a>
    </div>
  </div>
  </div>
	
</form>
<script>
    var  no,
    	 permission = CommonUtil.getPermission('${name_english}');
    $(function () {
    	no = 0;
    	
    	//初始化下拉选项
	   	 /*  $('#isAlert0').combobox({
	           valueField : 'code',
	           textField : 'name',
	           url : COMMON_URL.getCode + '?type=T_DM_T_PROJECT_COUPON_IS_ALERT'
	       });
    	 */
	   	  $('#type0').combobox({
	           valueField : 'code',
	           textField : 'name',
	           url : COMMON_URL.getCode + '?type=T_DM_T_PROJECT_COUPON_TYPE'
	       });
	   	
    	//初始化funcbar
     	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['${name_english}:add']
        }));
   	     	
    	//保存按钮
    	$('#funcbar').on('click', 'a[data-permit="${name_english}:add"]', {isContinue: false},  submit);
    	$("#imagePath0").filePlugin();

    	//返回按钮
    	$('#backBtn,#goBack').on('click',backMain);
    	$('#addBtn').on('click',addCoupon);
    });
    
    
    //表单提交
    function submit(event) {
    	var coupons=[];
    	$('#addNew div').each(function(i){
    		var param1 = $("div input[name='name"+i+"']").val(); 
    		var param2 = $("div input[name='time"+i+"']").val();
    		var param3 = $("div input[name='amount"+i+"']").val();
    		var param4 = $("div input[name='useCond"+i+"']").val();
    		var param5 = $("div input[name='startTime"+i+"']").val();
    		var param6 = $("div input[name='endTime"+i+"']").val();
    		var param8 = $("div input[name='imagePath"+i+"']").val();
    		var param10 = $("div input[name='count"+i+"']").val();
    		var param9 = $("div input[name='publicSign"+i+"']").val();
    		var param11 = $("div input[name='type"+i+"']").val();
    		if(typeof(param1) == "undefined"){
    			return false;
    		}else{
    			if(i==0){
            		coupons.push(param1+","+param2+","+param3+","+param4+","+param5+","+param6+","+param8+","+param9+","+param10+","+param11+"#");
        		}else{
            		coupons.push(param1+","+param2+","+param3+","+param4+","+param5+","+param6+","+param10+"#");
        		}
    		}
    		
    		});
		console.log(coupons);

    	/* $("#item").val(coupons);
    	var item = $("#item").val(); */
    	$("#coupons").val(coupons);
    	console.log($("#coupons").val());

         $("#addForm").form({
        	 buttons : ['#saveBtn','#saveBtnContinue'],
             ajax: {
                 url: CONST_URL.ADD,
                  data:{
        				"coupons" :$("#coupons").val()
        			 }  
             },
             onSubmitSuccess: function (data) {
            	 $.stip({
  	                type:'ok',//显示类型 有info、 ok、 danger
  	                text: '保存成功！',
  	                onBeforeHide: function () {
  	            	  var isContinue = event.data.isContinue;
                   	  if (isContinue) {
                          resetForm();
                   	  } else {
                   		  backMain();
                   	  }
  	               }
              	});
              }
         }).form("submit");
    }
    
    //保存并继续清空form
    function resetForm(){
        //$('#xm,#zw,#cs,#bgdh,#sjhm').val('');
    }
    function addCoupon(){
    	no++;
    	var html = "";
    	html = '<hr height:2px noshade=true/>'
    		+'<div class="form-cont">'
	        +'<p class="form-cont-title"><span class="field-required">*</span>红包名</p>'
	        +'<input type="text" id="name'+no+'" name="name'+no+'" class="summer-input summer-validatebox" data-validatebox="validType:[\'required\',\'maxLength[50]\']" /> '
	        +'</div>'
	     
	     	+'<div class="form-cont">'
	        +'<p class="form-cont-title"><span class="field-required">*</span>领取次数</p>'
	        +'<input type="text" id="time'+no+'" name="time'+no+'" class="summer-input summer-validatebox" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/> '
	        +'</div>'
	     
	      	+'<div class="form-cont">'
	        +'<p class="form-cont-title"><span class="field-required">*</span>金额</p>'
	        +'<input type="text" id="amount'+no+'" name="amount'+no+'" class="summer-input" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/>'
	        +'</div>'
	     
	     	+'<div class="form-cont">'
	        +'<p class="form-cont-title"><span class="field-required">*</span>使用条件</p>'
	        +'<input type="text" id="useCond'+no+'" name="useCond'+no+'" class="summer-input summer-validatebox" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/>' 
	        +'</div>'
	     
	     	+'<div class="form-cont">'
	        +'<p class="form-cont-title"><span class="field-required">*</span>起始时间</p>'
	        +'<input type="text" id="startTime'+no+'" name="startTime'+no+'" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:\'yyyy-MM-dd\'})" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/> '
	     	+'</div>'
	     
	        +'<div class="form-cont">'
	        +'<p class="form-cont-title"><span class="field-required">*</span>结束时间</p>'
	        +'<input type="text" id="endTime'+no+'" name="endTime'+no+'" class="summer-input summer-validatebox" onFocus="WdatePicker({dateFmt:\'yyyy-MM-dd\'})" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/>' 
	     	+'</div>'
	     
		     +'<div class="form-cont">'
		     +'<p class="form-cont-title"><span class="field-required">*</span>数量</p>'  
		     +'<input type="text" id="count'+no+'" name="count'+no+'" class="summer-input summer-validatebox" />'    
		     +'</div>'
		     ;
	     	$("#addNew").append(html);
    }
    //返回主页面
    function backMain() {
    	window.location.href = CONST_URL.INIT_MAIN;
    }
    
   
    //请求url
    var CONST_URL = {
    	INIT_MAIN: basePath + "${common_url}init",
       	ADD: basePath + "${common_url}add"
    };
    
</script>
</body>
</html>